/*
 * @description:
 * @Author: jhw
 * @Date: 2025-06-27 10:52:07
 * @LastEditors: jhw
 * @LastEditTime: 2025-08-14 22:12:19
 */
import { useMaterialStore } from '@/stores/useMaterial';
import { createRouter, createWebHistory } from 'vue-router';
import materials from './materials';
import HomeView from '@/views/HomeView.vue';
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
    },
    // 组件市场
    {
      path: '/materials',
      name: 'materials',
      redirect: '/materials/select-group',
      component: () => import('@/views/MaterialsView/Index.vue'),
      children: [...materials],
    },
    // 编辑器
    {
      path: '/editor',
      name: 'editor',
      redirect: '/editor/base-component',
      component: () => import('@/views/EditorView/Index.vue'),
      children: [
        {
          path: 'base-component',
          name: 'base-component',
          component: () => import('@/views/EditorView/LeftSide/BaseComponent.vue'),
        },
        {
          path: 'enhance-component',
          name: 'enhance-component',
          component: () => import('@/views/EditorView/LeftSide/EnhanceComponent.vue'),
        },
      ],
    },
    // 预览
    {
      path: '/preview',
      name: 'preview',
      component: () => import('@/views/preview.vue'),
    },
    // 提交答案
    {
      path: '/submit-answer/:id',
      name: 'submit-answer',
      component: () => import('@/views/submitAnswer.vue'),
    },
  ],
});

router.beforeEach((to, from, next) => {
  // 设置之前需要判断一下是否是组件市场
  // 因为只有组件市场需要记录当前的组件
  const activeView = localStorage.getItem('activeView');
  const materialStore = useMaterialStore();
  if (activeView === 'materials' && to.name) {
    materialStore.setCurrentMaterialCom(to.name as string);
  }

  next();
});

export default router;
